package com.maoye.ossp.common.web.taglib;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.commons.lang3.StringUtils;

import com.maoye.ossp.common.service.IDataBean;
import com.maoye.ossp.common.util.ApplicationContextUtils;
import com.maoye.ossp.common.util.RequestContext;
import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.utils.SysDictUtils;

/**
 * 自定义select标签java类
 * 
 * @author yanghao
 * @date 2014-01-10
 */
public class SelectTag extends BaseTag {

	private static final long serialVersionUID = 4938037565086504235L;

	private String name;
	private String dictType;
	private String value;
	private String defaultSelected;
	private String service;
	private String chosen;
	private String isShowSelectTips = Boolean.TRUE.toString();
	private String class_;

	/**
	 * @return the class_
	 */
	public String getClass_() {
		return class_;
	}

	/**
	 * @param class_ the class_ to set
	 */
	public void setClass_(String class_) {
		this.class_ = class_;
	}

	/**
	 * @return the chosen
	 */
	public String getChosen() {
		return chosen;
	}

	/**
	 * @param chosen
	 *            the chosen to set
	 */
	public void setChosen(String chosen) {
		this.chosen = chosen;
	}

	public String getDefaultSelected() {
		return defaultSelected;
	}

	public void setDefaultSelected(String defaultSelected) {
		this.defaultSelected = defaultSelected;
	}

	public int doStartTag() throws JspException {
		try {
			StringBuilder sb = new StringBuilder();
			if(StringUtils.isNotEmpty(class_)){
				sb.append("<select  class=\""+class_+"\" ");
			}else{
				sb.append("<select  class=\"f_5\" ");
			}
			if (value != null) {
				sb.append("value=\"");
				sb.append(value);
				sb.append("\"  ");
			}
			if (name != null) {
				sb.append("name=\"");
				sb.append(name);
				sb.append("\"  ");
			}
			if (StringUtils.isNotEmpty(chosen)) {
				sb.append("data-o-chosen=\"");
				sb.append(chosen);
				sb.append("\"  ");
			}

			generateAttribute(sb);
			sb.append(">");
			// sb.append("\n");
			if (Boolean.parseBoolean(isShowSelectTips)) {
				sb.append("<option value=\"\">--请选择--</option>");
			}
			// sb.append("\n");
			builderOptions(sb);
			sb.append("</select>");
			pageContext.getOut().write(sb.toString());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;

	}

	private void builderOptions(StringBuilder sb) throws Exception {
		if (StringUtils.isNotEmpty(service)) {
			RequestContext request = new RequestContext(
					(HttpServletRequest) pageContext.getRequest());
			String sBean = "";
			String params = "";
			String[] s = service.split("\\?");
			sBean = s[0];
			if (s.length > 1) {
				params = s[1];
				setParameter(request, params);
			}
			IDataBean dataBean = (IDataBean) ApplicationContextUtils
					.getBean(sBean);
			List<Object> dataList = dataBean.getDataList(request);
			for (int i = 0; i < dataList.size(); i++) {
				Map<String, Object> map = (Map<String, Object>) dataList.get(i);
				String id = map.get("id").toString();
				sb.append("<option value=\"").append(id).append("\" ");
				if (StringUtils.isNotEmpty(value)) {
					if (value.equals(id)) {
						sb.append(" selected ");
					}
				} else if (StringUtils.isNotEmpty(defaultSelected)) {
					if (defaultSelected.equals(id)) {
						sb.append(" selected ");
					}
				}
				sb.append(">").append(map.get("name")).append("</option>");
			}
		} else if (StringUtils.isNotEmpty(dictType)) {
			List<SysDictDetail> sysDictDetailList = SysDictUtils
					.getSysDictDetailList(dictType);
			for (SysDictDetail sysDictDetail : sysDictDetailList) {
				sb.append("<option value=\"").append(sysDictDetail.getKey())
						.append("\" ");
				if (StringUtils.isNotEmpty(value)) {
					if (value.equals(sysDictDetail.getKey())) {
						sb.append(" selected ");
					}
				} else if (StringUtils.isNotEmpty(defaultSelected)) {
					if (defaultSelected.equals(sysDictDetail.getKey())) {
						sb.append(" selected ");
					}
				}
				sb.append(">").append(sysDictDetail.getValue())
						.append("</option>");
			}
		}
	}

	private void setParameter(RequestContext request, String params) {

		String[] paramAry = params.split("&");
		for (int i = 0; i < paramAry.length; i++) {
			String[] param = paramAry[i].split("=");
			if (param.length > 1) {
				request.setParameter(param[0], param[1]);
				request.setAttribute(param[0], param[1]);
			}
		}
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDictType() {
		return dictType;
	}

	public void setDictType(String dictType) {
		this.dictType = dictType;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public String getIsShowSelectTips() {
		return isShowSelectTips;
	}

	public void setIsShowSelectTips(String isShowSelectTips) {
		this.isShowSelectTips = isShowSelectTips;
	}

}
